package com.lysj.bhp.announcement.controller;


import com.lysj.bhp.announcement.base.AnnouncementContent;
import com.lysj.bhp.announcement.domain.Announcement;
import com.lysj.bhp.announcement.vo.AnnouncementVo;
import com.lysj.bhp.article.domain.Article;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("admin/announcement")
public class AnnouncementController extends AnnouncementContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @GetMapping("list")
    public Resp list() {

        return new Resp(announcementRepository.findByIsExistOrderByDateDesc(1));
    }

    @PostMapping("add")
    public Resp add(Announcement model) {
        if (ParamUtil.isBlack(model.getChineseTitle())) {
            return new Resp(Resp.PARAM_ERROR, "中文标题不能为空");
        }
        if (ParamUtil.isBlack(model.getEnglishTitle())) {
            return new Resp(Resp.PARAM_ERROR, "英文标题不能为空");
        }
        if (ParamUtil.isBlack(model.getDate())) {
            return new Resp(Resp.PARAM_ERROR, "时间不能为空");
        }
        model.setIsExist(1);
        announcementRepository.save(model);
        return new Resp("添加成功");
    }

    @PostMapping("/edit")
    public Resp edit(Announcement model) {
        Announcement announcement = announcementRepository.findOne(model.getAnnouncementId());
        if (announcement == null) {
            return new Resp(Resp.PARAM_ERROR, "公告不存在!");
        }
        if (ParamUtil.isBlack(model.getChineseTitle())) {
            return new Resp(Resp.PARAM_ERROR, "中文标题不能为空");
        }
        if (ParamUtil.isBlack(model.getEnglishTitle())) {
            return new Resp(Resp.PARAM_ERROR, "英文标题不能为空");
        }
        if (ParamUtil.isBlack(model.getDate())) {
            return new Resp(Resp.PARAM_ERROR, "时间不能为空");
        }

        announcement.setChineseTitle(model.getChineseTitle());
        announcement.setEnglishTitle(model.getEnglishTitle());
        announcement.setDate(model.getDate());
        announcementRepository.save(announcement);
        return new Resp("修改成功");
    }


    @PostMapping("/remove")
    public Resp remove(Announcement model) {
        if (ParamUtil.isBlack(model.getAnnouncementId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择公告");
        }
        Announcement announcement = announcementRepository.findOne(model.getAnnouncementId());
        if (null == announcement) {
            return new Resp(Resp.PARAM_ERROR, "查无此公告");
        }
        announcement.setIsExist(2); //设定状态为2,用于逻辑删除
        announcementRepository.save(announcement);
        return new Resp("删除成功");
    }


    @PostMapping("/editChineseContent")
    public Resp editChineseContent(Long announcementId, String chineseContent) {
        if (ParamUtil.isBlack(announcementId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择公告");
        }
        Announcement announcement = announcementRepository.findOne(announcementId);
        if (null == announcement) {
            return new Resp(Resp.PARAM_ERROR, "查无此公告");
        }

        announcement.setChineseContent(chineseContent);
        announcementRepository.save(announcement);
        return new Resp("添加成功");
    }

    @PostMapping("/editEnglishContent")
    public Resp editEnglishContent(Long announcementId, String englishContent) {
        if (ParamUtil.isBlack(announcementId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择公告");
        }
        Announcement announcement = announcementRepository.findOne(announcementId);
        if (null == announcement) {
            return new Resp(Resp.PARAM_ERROR, "查无此公告");
        }

        announcement.setEnglishContent(englishContent);
        announcementRepository.save(announcement);
        return new Resp("添加成功");
    }


}
